/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package knn;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Nguyen Thuy Ngoc <ntngoc1988@gmail.com>
 */
public class DefaultDominantSelector<T extends ISample> implements IDominantSelector<T> {

    @Override
    public String determineDominantClass(T[] samples, double[] distances) {
        Map<String, Integer> countPerClass = new HashMap<String, Integer>();

        String dominantClass = null;
        Integer dominantCount = 0;

        for (T sample : samples) {
            if (countPerClass.containsKey(sample.getAssignedClass())) {
                Integer count = countPerClass.get(sample.getAssignedClass()) + 1;
                countPerClass.put(sample.getAssignedClass(), count);
            } else {
                countPerClass.put(sample.getAssignedClass(), 1);
            }

            if (countPerClass.get(sample.getAssignedClass()) > dominantCount) {
                dominantClass = sample.getAssignedClass();
                dominantCount = countPerClass.get(sample.getAssignedClass());
            }
        }
        return dominantClass;
    }
}
